package helpers;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import models.Company;
import models.User;
import play.mvc.Http.Context;
import play.mvc.Http.Session;
import controllers.Application;

public class ConsoleUtils {
	
	public static String getPath(String[] directoryNames) {
		if (directoryNames != null && directoryNames.length > 0) {
			StringBuilder path = new StringBuilder();
			for (String directoryName : directoryNames) {
				path.append(directoryName).append(ConsoleConstants.PATH_SEPARATOR);
			}
			return path.substring(0, path.length() - ConsoleConstants.PATH_SEPARATOR.length());
		}
		return null;
	}
	
	public static boolean hasFileExtension(String fileName, String[] fileExtensions) {
		boolean result = false;
		if (fileName != null && fileName.trim() != "" && fileExtensions != null && fileExtensions.length > 0) {
			for (String fileExtension : fileExtensions) {
				if (fileName.endsWith(fileExtension)) {
					result = true;
					break;
				}
			}
		}
		return result;
	}
	
	public static String getFileContent(File file) {
		String content = null;
		if (file != null && file.exists()) {
			BufferedReader reader = null;
			try {
				reader = new BufferedReader(new FileReader(file));
				StringBuilder builder = new StringBuilder();
				String line = reader.readLine();
				
				while (line != null) {
					builder.append(line).append("\n");
					line = reader.readLine();
				}
				content = builder.toString();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				if (reader != null) {
					try {
						reader.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
		return content;
	}
	
	public static Company getCompany() {
		final Context context = Context.current();
		final Session session = context.session();
		final User user = Application.getLocalUser(session);
		return user.getCompany();
	}
	
}
